﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ChinaClipping.ClippingMaster.MyTools
{
    public class UploadHistory
    {
        private static string _historyfile="";
        private static StringBuilder _his;
        public static event HistoryAddedEventHandler HistoryAdded;
        /// <summary>
        /// 添加历史记录
        /// </summary>
        /// <param name="url"></param>
        public static void AddHistory(string url)
        {
            if (string.IsNullOrEmpty(url))
                return;
            url = url.Trim().ToLower();
            if (_his == null)
                _his = new StringBuilder();
            if (!InHistory(url))
            {
                string md5 = Hash.md5Hash(url);
                _his.AppendLine(md5);
                if (HistoryAdded != null)
                    HistoryAdded(new HistoryAddedEventArgs(url));
                try
                {
                    File.AppendAllText(_historyfile, md5 + Environment.NewLine);
                }
                catch { }
            }
        }
        /// <summary>
        /// 判断是否存在指定URL的历史记录
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool InHistory(string url)
        {
            if (string.IsNullOrEmpty(url))
                return false;
            string md5 = Hash.md5Hash(url.Trim().ToLower());
            if (_his == null)
                return false;
            else
            {
                return Regex.IsMatch(_his .ToString (),string.Format ("{0}\\s",md5));
            }

        }
        /// <summary>
        /// 设置历史记录文件
        /// </summary>
        public static string HistoryFile
        {
            set 
            {
                _historyfile = value;
                loadhis(value);
            }
        }

        private static void loadhis(string file)
        {
            if (File.Exists(file))
            {
                try
                {
                    string history = File.ReadAllText(file);
                    _his = new StringBuilder(history);
                }
                catch { }
            }
            else
            {
                _his = new StringBuilder();
            }
        }

        /// <summary>
        /// 获取历史记录条数
        /// </summary>
        public static int RecordsCount
        {
            get 
            {
                if (_his != null)
                    return Regex.Matches(_his.ToString(), "\\s+").Count;
                else
                    return 0;
            }
        }

        /// <summary>
        /// 保留指定条数的历史记录，清除其余
        /// </summary>
        /// <param name="reservedCount"></param>
        public static void ClearHistory(int reservedCount)
        {
        int currentCount=RecordsCount ;
        int dif = currentCount - reservedCount;
        if (dif > 0)
        {
            int index = Regex.Matches(_his.ToString(), "\\s+")[dif].Index;
            _his.Remove(0, index);
            try
            {
                File.WriteAllText(_historyfile, _his.ToString());
            }
            catch { }
        }
        }
    }

    public class HistoryAddedEventArgs : EventArgs
    {
        private string _url;
        public HistoryAddedEventArgs(string url)
        {
            _url = url;
        }
        public string Url
        {
            get { return this._url; }
        }
    }

    public delegate void HistoryAddedEventHandler(HistoryAddedEventArgs e);
}
